#include "settings.h"
#include <string>
#include <iostream>

using namespace std;



Settings::Settings(int argc, char *argv[]) {

#	if 0
	/* set up fake command-line argument string */
	char *cmdString[7];
	cmdString[ 0] = "eg";
	cmdString[ 1] = "-i";
	cmdString[ 2] = "/Users/johnh/Working/like/bglobin.in";
	cmdString[ 3] = "-t";
	cmdString[ 4] = "/Users/johnh/Working/like/bglobin.tree";
	cmdString[ 5] = "-o";
	cmdString[ 6] = "/Users/johnh/Working/like/bglobin.out";
	argc = 7;
	argv = cmdString;
#	endif

	enum Mode { DATA_FILE, TREE_FILE, OUTPUT_FILE, NONE };

	/* set default values for parameters */
	dataFileName           = "";
	outFileName            = "";
	treeFileName           = "";
	
	if (argc > 1)
		{
		if (argc % 2 == 0)
			{
			cout << "Usage:" << endl;
			cout << "   -i : Input file name" << endl;
			cout << "   -t : Tree file name" << endl;
			cout << "   -o : Output file name" << endl;
			cout << "Example:" << endl;
			cout << "   r88s -i <input file> -t <tree file> -o <output file>" << endl;
			exit(0);
			}
			
		/* read the command-line arguments */
		int status = NONE;
		for (int i=1; i<argc; i++)
			{
			string cmd = argv[i];
			//cout << cmd << endl;
			if (status == NONE)
				{
				/* read the parameter specifier */
				if ( cmd == "-i" )
					status = DATA_FILE;
				else if ( cmd == "-t" )
					status = TREE_FILE;
				else if ( cmd == "-o" )
					status = OUTPUT_FILE;
				else
					{
					cerr << "Could not interpret option \"" << cmd << "\"." << endl;
					exit(1);
					}
				}
			else
				{
				/* read the parameter */
				if ( status == DATA_FILE )
					dataFileName = argv[i];
				else if ( status == TREE_FILE )
					treeFileName = argv[i];
				else if ( status == OUTPUT_FILE )
					outFileName = argv[i];
				else
					{
					cerr << "Unknown status reading command line information" << endl;
					exit(1);
					}
				status = NONE;
				}
			}
		}
	else
		{
		/* set the parameters from the standard in */
		cout << "   Program settings (Note: This program supports command line specification of settings)" << endl;
		cout << "      Data file name        = ";
		cin >> dataFileName;
		cout << "      Tree file name        = ";
		cin >> treeFileName;
		cout << "      Output file name      = ";
		cin >> outFileName;
		cout << endl;
		}	

}
